﻿using BLL;
using BONGDA.Excel;
using DTO;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BONGDA
{
    public partial class frmReferee : Form
    {
        public frmReferee()
        {
            InitializeComponent();
        }
        int maTrongTai;
        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void frmReferee_Load(object sender, EventArgs e)
        {
            HienThiDS();
        }
        private void HienThiDS()
        {
            DataTable dt = TrongTai_BLL.SelectAll();
            dgvTrongTai.DataSource = dt;
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            frmAddReferee frm = new frmAddReferee();
            frm.ShowDialog();
            HienThiDS();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            try
            {
                frmUpReferee frm = new frmUpReferee(maTrongTai);
                frm.ShowDialog();
                HienThiDS();
            }
            catch (Exception)
            {
                MessageBox.Show("Chọn TRỌNG TÀI muốn chỉnh sữa!", "Thông báo",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Warning);
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {           
            try
            {
                TrongTai_DTO dto = TrongTai_BLL.SelectByID(Convert.ToInt32(dgvTrongTai.SelectedRows[0].Cells[0].Value));
                DialogResult kq;
                kq = MessageBox.Show("Xóa TRỌNG TÀI \"" + dto.TenTrongTai + "\"?", "Thông báo",
                MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (kq == DialogResult.OK)
                {
                    TrongTai_BLL.Delete(maTrongTai);
                    HienThiDS();
                    MessageBox.Show("Xóa TRỌNG TÀI thành công!",
                                "Thông báo",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Chọn TRỌNG TÀI muốn xóa!",
                                "Thông báo",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Warning);
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnIm_Click(object sender, EventArgs e)
        {
            
        }

        private void btnEx_Click(object sender, EventArgs e)
        {
            try
            {
                string oldPath = Directory.GetCurrentDirectory();
                SaveFileDialog sf = new SaveFileDialog();
                sf.ShowDialog();
                string strFileName = sf.FileName;
                Directory.SetCurrentDirectory(oldPath);
                ExcelWrite myExcel = new ExcelWrite();
                List<String> list = myExcel.DGVToExcel(dgvTrongTai);
                String filePath = Directory.GetCurrentDirectory() + strFileName;
                myExcel.WriteDateToExcel(strFileName, list, "A1", "F1", "A2");
                MessageBox.Show("Export dữ liệu thành công!", "Thông báo",
                                MessageBoxButtons.OK, MessageBoxIcon.Information);
                Process.Start(strFileName);
            }
            catch (Exception)
            {
            }
            
        }

        private void dgvTrongTai_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                if (dgvTrongTai.SelectedRows.Count > 0)
                {
                    // Thông tin đội bóng
                    TrongTai_DTO dto = TrongTai_BLL.SelectByID(Convert.ToInt32(dgvTrongTai.SelectedRows[0].Cells[0].Value));
                    maTrongTai = dto.MaTrongTai;
                }
            }
            catch (Exception)
            {
            }    
        }

        private void txtTimKiem_TextChanged(object sender, EventArgs e)
        {
            dgvTrongTai.DataSource = TrongTai_BLL.SelectLikeTen(txtTimKiem.Text.ToString());
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            txtTimKiem.Text = "";
        }
    }
}
